﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Xml;

namespace PositionMeasureSystem
{
    public struct LocSize
    {
        public int x, y, dx, dy;
    }
    public class FigPanel
    {
        //PosiModel posiModel = new PosiModel();
        public string Name;
        public MyColor mColor = new MyColor();
        public List<Chart> Charts = new List<Chart>();
        public Control Parent;// = new Control();
        public Panel mPanel = new Panel();
        public XmlNode PanelPara;
        public int TotalItemDataNum;
        public List<DataItem> DataItems = new List<DataItem>();
        public List<AxisPara> ChartAxis = new List<AxisPara>();
        public List<ScottPlot.FormsPlot> Figs = new List<ScottPlot.FormsPlot>();

        private static Chart chartBot;
        public static Chart ChartBot { get => chartBot; set => chartBot = value; }
        
        public FigPanel (XmlNode monitor)
        {
            PanelPara = monitor;
            XmlNode panelname = monitor.SelectSingleNode("Name");
            if (panelname.InnerText != "")
                Name = panelname.InnerText;
            else
                Name = "FigPanel";
        }
        public static int CreatFigsBot (XmlNode monitor, ref FigPanel figPanel)
        {
            
            if (monitor == null)
                return -1;

            int figtcnt = 0;

            XmlNode figs = monitor.SelectSingleNode("Figs");
            XmlNodeList figsnode = figs.ChildNodes;
            
            foreach (XmlNode fignode in figsnode)
            {
                if (fignode.Name == "Fig")//一个图形
                {
                    XmlNode locnode = fignode.SelectSingleNode("Location");
                    LocSize panelloc;
                    //                    panelloc.x = parent.Location.X;
                    panelloc.x = figPanel.mPanel.Location.X;
                    panelloc.y = figPanel.mPanel.Location.Y;
                    panelloc.dx = figPanel.mPanel.Size.Width;
                    panelloc.dy = figPanel.mPanel.Size.Height;
                    LocSize figloc;
                    if (locnode != null)
                    {
                        if (locnode.ChildNodes.Count == 4)
                        {
                            figloc.x = Convert.ToInt32(locnode.ChildNodes[0].InnerText);
                            figloc.y = Convert.ToInt32(locnode.ChildNodes[1].InnerText);
                            figloc.dx = Convert.ToInt32(locnode.ChildNodes[2].InnerText);
                            figloc.dy = Convert.ToInt32(locnode.ChildNodes[3].InnerText);
                        }
                        else //暂时简化
                        {
                            figloc = AutoPosSize(figs.ChildNodes.Count, figtcnt, panelloc, 0);
                        }
                    }
                    else
                    {
                        figloc = AutoPosSize(figs.ChildNodes.Count, figtcnt, panelloc, 0);
                    }
                    //List<SensorData> sensorDatas1=sensorDatas2;
                    Chart fig = CreatFigBot(fignode, figloc, ref figPanel);

                    figPanel.Charts.Add(fig);
                    figtcnt++;
                }

            }
            return figtcnt;
        }
        public static Chart CreatFigBot (XmlNode fignode, LocSize loc, ref FigPanel figPanel)
        {
            int index = 0;
            int i = 0;
            
            XmlNode curveroot = fignode.SelectSingleNode("CurveRoot");
            XmlNode chartname = fignode.SelectSingleNode("Name");
            chartBot = new Chart();
            ChartArea figarea;
            if (chartname != null)
                figarea = new ChartArea(chartname.InnerText);
            else
                figarea = new ChartArea();

            chartBot.ChartAreas.Add(figarea);
            //chartBot.ChartAreas[0].AxisX.IntervalOffset = 4.00D;
            chartBot.Location = new Point(loc.x, loc.y);
            chartBot.Width = loc.dx;
            chartBot.Height = loc.dy;
            chartBot.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;

            XmlNodeList curves = curveroot.ChildNodes;
            
            if (curves != null)
            {
                foreach (XmlNode curve in curves)
                {
                    if (curve.Name == "Curve")
                    {
                        Series series = new Series();
                        series.ChartType = SeriesChartType.Line;
                        series.Color = figPanel.mColor.c[index];
                        chartBot.Series.Add(series);
                        curveBot(chartBot, index);
                        i++;
                        index++;
                    }
                }
            }
            figPanel.mPanel.Controls.Add(chartBot);
            chartBot.Show();
            return chartBot;
        }
        public static void curveBot (Chart chart, int si)
        {
            float y;
            for (int i = 0; i < Form1.dataNum - 1; i++)
            {

                y = (float)Form1.posHisSensorData[si][i];
                chart.Series[si].Points.AddXY(i, y);
            }
        }
        static int figCntTop = 0;
        public static int CreatFigsTop (XmlNode monitor, ref FigPanel figPanel)
        {

            if (monitor == null)
                return -1;
            XmlNode figs = monitor.SelectSingleNode("Figs");
            XmlNodeList figsnode = figs.ChildNodes;

            foreach (XmlNode fignode in figsnode)
            {
                if (fignode.Name == "Fig")//一个图形
                {
                   
                    XmlNode locnode = fignode.SelectSingleNode("Location");
                    LocSize panelloc;
                    //                    panelloc.x = parent.Location.X;
                    panelloc.x = figPanel.mPanel.Location.X;
                    panelloc.y = figPanel.mPanel.Location.Y;
                    panelloc.dx = figPanel.mPanel.Size.Width;
                    panelloc.dy = figPanel.mPanel.Size.Height;
                    LocSize figloc;
                    if (locnode != null)
                    {
                        if (locnode.ChildNodes.Count == 4)
                        {
                            figloc.x = Convert.ToInt32(locnode.ChildNodes[0].InnerText);
                            figloc.y = Convert.ToInt32(locnode.ChildNodes[1].InnerText);
                            figloc.dx = Convert.ToInt32(locnode.ChildNodes[2].InnerText);
                            figloc.dy = Convert.ToInt32(locnode.ChildNodes[3].InnerText);
                        }
                        else //暂时简化
                        {
                            figloc = AutoPosSize(figs.ChildNodes.Count, figCntTop, panelloc, 0);
                        }
                    }
                    else
                    {
                        figloc = AutoPosSize(figs.ChildNodes.Count, figCntTop, panelloc, 0);
                    }
                    Chart fig = CreatFigTop(fignode, figloc, ref figPanel);
                    figPanel.Charts.Add(fig);
                    figCntTop++;
                }

            }
            return figCntTop;
        }
        public static Chart CreatFigTop (XmlNode fignode, LocSize loc, ref FigPanel figPanel)
        {
            int index = 0;
            int i = 0;

            XmlNode curveroot = fignode.SelectSingleNode("CurveRoot");
            XmlNode chartname = fignode.SelectSingleNode("Name");
            Chart chartTop = new Chart();
            ChartArea figarea;
            if (chartname != null)
                figarea = new ChartArea(chartname.InnerText);
            else
                figarea = new ChartArea();

            chartTop.ChartAreas.Add(figarea);
            //chartTop.ChartAreas[0].AxisX.IntervalOffset = 1.00D;
            chartTop.Location = new Point(loc.x, loc.y);
            chartTop.Width = loc.dx;
            chartTop.Height = loc.dy;
            chartTop.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
            chartTop.Titles.Add(new System.Windows.Forms.DataVisualization.Charting.Title());
            

            XmlNodeList curves = curveroot.ChildNodes;
            if (curves != null)
            {
                foreach (XmlNode curve in curves)
                {
                    if (curve.Name == "Curve")
                    {
                        Series series = new Series();
                        series.ChartType = SeriesChartType.Line;
                        series.Color = figPanel.mColor.c[figCntTop];
                        chartTop.Series.Add(series);
                        curveTop(chartTop, figCntTop);
                        i++;
                        index++;
                    }
                }
            }
            figPanel.mPanel.Controls.Add(chartTop);
            chartTop.Show();
            return chartTop;
            
        }
        public static void curveTop (Chart chart, int si)
        {
            float y;
            for (int i = 0; i < Form1.dataNum-1; i++)
            {

                y = (float)Form1.posHisSensorData[si][i];
                chart.Series[0].Points.AddXY(i, y);
            }
        }
        public static LocSize AutoPosSize (int totalnum, int index, LocSize panellocsize, int type)//
        {
            if (index < 0)
                index = 0;
            if (totalnum > 16)
                totalnum = 16;

            LocSize locsize;
            locsize.x = panellocsize.x;
            locsize.y = panellocsize.y;

            if (totalnum < 2)
            {
                locsize.dx = panellocsize.dx;
                locsize.dy = panellocsize.dy;
                locsize.x = panellocsize.x;
                locsize.y = panellocsize.y;
            }
            else if (totalnum < 3)
            {
                locsize.dx = panellocsize.dx;
                locsize.dy = panellocsize.dy / 2;
                locsize.x = panellocsize.x;
                locsize.y += locsize.dy * index;
            }
            else if (totalnum < 4)
            {
                if (index < 2)
                    locsize.dx = panellocsize.dx / 2;
                else
                    locsize.dx = panellocsize.dx;
                locsize.dy = panellocsize.dy / 2;
                locsize.x += locsize.dx * (index % 2);
                locsize.y += locsize.dy * (index / 2);
            }
            else if (totalnum < 5)
            {
                /*locsize.dx = panellocsize.dx / 2;
                locsize.dy = panellocsize.dy / 2;
                locsize.x += locsize.dx * (index % 2);
                locsize.y += locsize.dy * (index / 2);*/
                locsize.dx = panellocsize.dx / totalnum;
                locsize.dy = panellocsize.dy;
                locsize.x = locsize.dx * index;
                locsize.y = 0;
            }
            else if (totalnum < 6)
            {
                if (index < 4)
                    locsize.dx = panellocsize.dx / 2;
                else
                    locsize.dx = panellocsize.dx;
                locsize.dy = panellocsize.dy / 3;
                locsize.x += locsize.dx * (index % 2);
                locsize.y += locsize.dy * (index / 2);
            }
            else if (totalnum < 7)
            {
                locsize.dx = panellocsize.dx / 2;
                locsize.dy = panellocsize.dy / 3;
                locsize.x += locsize.dx * (index % 2);
                locsize.y += locsize.dy * (index / 2);
            }
            else if (totalnum < 8)
            {
                if (type != 0)
                {
                    locsize.dy = panellocsize.dy / 3;
                    if (index < 3)
                    {
                        locsize.dx = panellocsize.dx / 3;
                        locsize.x += locsize.dx * (index % 3);
                    }
                    else
                    {
                        locsize.dx = panellocsize.dx / 2;
                        locsize.x += locsize.dx * ((index - 3) % 2);
                        locsize.y += locsize.dy * ((index - 3) / 2 + 1);
                    }
                }
                else
                {
                    if (index < 6)
                    {
                        locsize.dx = panellocsize.dx / 3;
                        locsize.x += locsize.dx * (index % 3);
                    }
                    else
                        locsize.dx = panellocsize.dx;
                    locsize.dy = panellocsize.dy / 3;
                    locsize.y += locsize.dy * (index / 3);
                }
            }
            else if (totalnum < 9)
            {
                locsize.dy = panellocsize.dy / 3;
                if (index < 6)
                {
                    locsize.dx = panellocsize.dx / 3;
                    locsize.x += locsize.dx * (index % 3);
                    locsize.y += locsize.dy * (index / 3);
                }
                else
                {
                    locsize.dx = panellocsize.dx / 2;
                    locsize.x += locsize.dx * ((index - 6) % 2);
                    locsize.y += locsize.dy * ((index - 6) / 2 + 2);
                }
            }
            else if (totalnum < 10)
            {
                locsize.dx = panellocsize.dx / 3;
                locsize.dy = panellocsize.dy / 3;
                locsize.x += locsize.dx * (index % 3);
                locsize.y += locsize.dy * (index / 3);
            }
            else if (totalnum < 11)
            {
                locsize.dy = panellocsize.dy / 3;
                if (index < 4)
                {
                    locsize.dx = panellocsize.dx / 4;
                    locsize.x += locsize.dx * (index % 4);
                }
                else
                {
                    locsize.dx = panellocsize.dx / 3;
                    locsize.x += locsize.dx * ((index - 4) % 3);
                    locsize.y += locsize.dy * ((index - 4) / 3 + 1);
                }
            }
            else if (totalnum < 12)
            {
                locsize.dy = panellocsize.dy / 3;
                if (index < 8)
                {
                    locsize.dx = panellocsize.dx / 4;
                    locsize.x += locsize.dx * (index % 4);
                    locsize.y += locsize.dy * (index / 4);
                }
                else
                {
                    locsize.dx = panellocsize.dx / 3;
                    locsize.x += locsize.dx * ((index - 8) % 3);
                    locsize.y += locsize.dy * ((index - 8) / 3 + 2);
                }
            }
            else if (totalnum < 13)
            {
                locsize.dx = panellocsize.dx / 4;
                locsize.dy = panellocsize.dy / 3;
                locsize.x += locsize.dx * (index % 4);
                locsize.y += locsize.dy * (index / 4);
            }
            else//超过16按16算，都重复在第16个图
            {
                locsize.dx = panellocsize.dx / 4;
                locsize.dy = panellocsize.dy / 4;
                locsize.x += locsize.dx * (index % 4);
                locsize.y += locsize.dy * (index / 4);
            }
            return locsize;
        }
        

    }
    public class AxisPara
    {
        public double min, max, grid;
        public int gridnum;
    };
    public class MyColor
    {
        public Color[] c = new Color[64];
        public MyColor ()
        {
            c[0] = Color.Red;
            c[1] = Color.Blue;
            c[2] = Color.Lime;
            c[3] = Color.DarkRed;
            c[4] = Color.Magenta;
            c[5] = Color.Cyan;
            c[6] = Color.DarkOrange;
            c[7] = Color.Black;
            c[8] = Color.Green;

            c[9] = Color.DarkViolet;
            c[10] = Color.DarkBlue;
            c[11] = Color.SpringGreen;
            c[12] = Color.DimGray;
            c[13] = Color.DeepPink;
            c[14] = Color.DodgerBlue;
            c[15] = Color.Yellow;
            c[16] = Color.DarkCyan;
            c[17] = Color.DarkGoldenrod;

            c[18] = Color.Purple;
            c[19] = Color.SlateBlue;
            c[20] = Color.LimeGreen;
            c[21] = Color.Olive;
            c[22] = Color.Violet;
            c[23] = Color.Turquoise;
            c[24] = Color.Gold;
            c[25] = Color.LightBlue;
            c[26] = Color.Tan;

            c[27] = Color.SaddleBrown;
            c[28] = Color.RoyalBlue;
            c[29] = Color.YellowGreen;
            c[30] = Color.DarkGreen;
            c[31] = Color.Thistle;
            c[32] = Color.SteelBlue;
            c[33] = Color.DeepSkyBlue;
            c[34] = Color.LightSeaGreen;
            c[35] = Color.SandyBrown;

            c[36] = Color.OrangeRed;
            c[37] = Color.LightSteelBlue;
            c[38] = Color.OliveDrab;
            c[39] = Color.Coral;
            c[40] = Color.LightCoral;
            c[41] = Color.YellowGreen;
            c[42] = Color.DarkKhaki;
            c[43] = Color.MediumSeaGreen;
            c[44] = Color.Goldenrod;

            //gColor[45] = Color;
            //gColor[46] = Color;
            //gColor[47] = Color;
            //gColor[48] = Color;
            //gColor[49] = Color;
            //gColor[50] = Color;
            //gColor[51] = Color;
            //gColor[52] = Color;
            //gColor[53] = Color;

            //gColor[54] = Color;
            //gColor[55] = Color;
            //gColor[56] = Color;
            //gColor[57] = Color;
            //gColor[58] = Color;
            //gColor[59] = Color;
            //gColor[60] = Color;
            //gColor[61] = Color;
            //gColor[62] = Color;

            //gColor[63] = Color;
        }
    }
    public class FigDat
    {
        public double[] dd;
        public int index = 0;
        public FigDat (long len)
        {
            dd = new double[len];
        }
    }
    public class SamplingData
    {
        public double time = 0;
        public float[] df;
        public int index = 0;
        public SamplingData (long len)
        {
            df = new float[len];
        }
    }

}
